﻿@model IEnumerable<ExpertSMS.Models.StoreToKitchenEntryItem>
           @{
               ExpertSMS.Models.ExpertSMSContext context = new ExpertSMS.Models.ExpertSMSContext();
               ExpertSMS.Models.StoreToKitchenEntry storeToKitchenEntry = new ExpertSMS.Models.StoreToKitchenEntry();
               
               if (Model.Count() > 0)
               {
                    storeToKitchenEntry = Model.FirstOrDefault().StoreToKitchenEntry;
               }
               bool isOK = true;
           }
<table>
    <tr>
        <th>สินค้า</th>
        <th>จำนวน</th>
        <th>หน่วย</th>
        <th>สินค้าใน Stock</th>
        <th>ต้นทุน</th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        string color = "Red";
        IEnumerable<ExpertSMS.Models.StoreProductStock> storeProductStocks = context.StoreProductStocks.Where(x=>x.ProductID == item.ProductID);


        decimal quantity = 0;
        decimal cost = 0;
        if (storeProductStocks.Count() > 0)
        {
            if (storeProductStocks.Sum(x => x.CurrentQuantity * item.Unit.ConvertingFactorToMainUnit) < item.Quantity)
            {
                color = "Red";
            }
            else
            {
                color = "Green";
            }
            foreach (var stock in context.StoreProductStocks.Where(x => x.ProductID == item.ProductID && x.CurrentQuantity > 0))
            {
                decimal costPerUnit = (stock.Cost / stock.OriginalQuantity);
                cost += (costPerUnit * stock.CurrentQuantity);
                quantity += stock.CurrentQuantity;
            }
        }
        if (color == "Red")
        {
            isOK = false;
        }
        <tr>
            <td>
                @Html.ActionLink(item.Product.Name,"Details","Products",new{id=item.ProductID},null)
            </td>
            <td class="right">@item.Quantity.ToString("n2")</td>
            <td>@item.Unit.Name</td>
            <td style="color: @color;">@((quantity * item.Unit.ConvertingFactorToMainUnit).ToString("n2")) @item.Unit.Name</td>
            <td>
            @if (quantity > 0)
            {
                <text>
                    @((((cost / quantity) / item.Unit.ConvertingFactorToMainUnit) * item.Quantity).ToString("n2")) บาท/ @item.Quantity.ToString("n2") @item.Unit.Name
                </text>
            }
            else
            {
                <text>ไม่มีสินค้าใน Stock</text>
            }
            </td>
            
            
            
            
            
                <td>[ @Ajax.ActionLink("ลบ", "DeleteConfirmed", "StoreToKitchenEntryItems", new { id = item.StoreToKitchenEntryItemID },
                new AjaxOptions { LoadingElementId = "loading", HttpMethod = "Get", UpdateTargetId = "productlist" }, null) ]
            </td>
            
        </tr>
    }
</table>
<br />
@if (Model != null && !Model.FirstOrDefault().StoreToKitchenEntry.IsConfirmed)
{
<div id="productdetails">
                @Html.Partial("_StoreToKitchenEntryProductDetails", storeToKitchenEntry.StoreToKitchenEntryID)
</div>
    if (isOK)
    {
        using (Html.BeginForm("Confirm", "StoreToKitchenEntries", FormMethod.Get))
        {
            @Html.Hidden("id", Model.FirstOrDefault().StoreToKitchenEntryID)
            <input type="submit" value="ยืนยันและทำการตัด Stock" />
        }
    }
    else
    {
        <p>
            <red>สินค้าใน Stock ไม่พอส่ง</red>
        </p>
    }
}